<script type="text/javascript">
$(function(){
	var pg = App.log.logsView.ui, pp = pg.params
		, tabs = pg.children('.tabs')
		, menuBtns = $('#head>.menuBtns');
	
	tabs.mac('tabs', {
		tabWidth: 200, //Use fix width
		tabHeight: 32,
		items: []
	});
	
	function query(p){
		var w = mac.wait('Loading'); 
		$.get(App.ctx + pp.path + '/query.do', {
			target: p.target,
			addr: p.addr,
			regx: p.regx,
			key: p.key,
			threadName: p.thread,
			traceId: p.trace,
			startTime: p.start,
			endTime: p.end,
			pageSize: p.limit
		}, function(rs) {
			w.close();
			var ro = mac.eval(rs);
			if (ro.success) {
				var rd = ro.data;
				tabs.closeTabs();
				for (var a in rd) {
					var n = a.replace(/\./g, '-').replace(/:/g, '-'),
						x = $('<pre class="logs"></pre>').attr('name', n).append(rd[a]);
					tabs.addTab({
						code: 'tab-' + n,
						title: a,
						closeable: true,
						height: '100%',
						el: x
					});
				}
				tabs.adjust().selectFirst();
			}
		});
	}

// 	$('#head').hide().children('.backBtn').unbind()
// 		.click(pg.callback || $.noop).toggle(pg.callback);
	var mainMenu = pg.children('.menu').mac('menu', {
		children: [{
			title : '自定义查询',
			action : 'query',
			icon: 'icon-newwin'
		},{
			title : '另存为',
			action: 'save',
			icon: 'icon-disk'
		}]
	}).appendTo($('body'));

	menuBtns.children('.menuBtn').contextMenu({
		menu: mainMenu,
		button: 1,
		anchor: 'el',
		offset: { x: 4, y: 60 }
	}, function(act, el, pos) {
		switch (act) {
		case 'saveAs':
			//
			break;
		case 'query':
			App.open({
				ctx: 'log',
				action: 'logsQuery',
				params: pp
			}, query);
			break;
		}
		
		return false;
	}).show();

	var frm = {
		path: pg.seek('path'),
		target: pg.seek('target'),
		addr: pg.seek('addr'),
		regx: pg.seek('regx'),
		key: pg.seek('key'),
		threadName: pg.seek('thread'),
		traceId: pg.seek('trace'),
		startTime: pg.seek('start'),
		endTime: pg.seek('end'),
		pageSize: pg.seek('limit'),
		collapse: pg.seek('collapse'),
		expand: pg.seek('expand')
	}
	frm.path.mac('combo', {
		width: 152,
		boxWidth: 153,
		readOnly: 1,
		data: [ '/es/index']
	});

	if (!pp) {
		pp = {
			path: App.getHashParam('path') || '/es/index',
			target: App.getHashParam('target') || 'logs',
			addr: App.getHashParam('addr'),
			thread: App.getHashParam('thread'),
			trace: App.getHashParam('trace'),
			start: App.getHashParam('start'),
			end: App.getHashParam('end'),
			key: App.getHashParam('key'),
			regx: App.getHashParam('regx'),
			logger: App.getHashParam('logger'),
			limit: App.getHashParam('limit') || 200,
			trace: App.getHashParam('trace')
		}
	}
	query(pp);

	function adjust(){
		var p = pg.parent(), h = p.height();
		pg.height(h - 2);
		tabs.height(h - 8).adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="tabs"></div>
<div class="menu"></div>